package client.comm.baoding.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.comm.commlib.network.data.JsonResult
import client.comm.baoding.api.bean.SeartchRet
import client.comm.baoding.ui.viewmodel.MiddleVm

class SearchRetModel(app: Application) : MiddleVm(app) {

    val wordVal = MutableLiveData<String>("")
    val isOneLoading = MutableLiveData<Boolean>(false)
    val searchType = MutableLiveData<String>("商品")
    val currMode = MutableLiveData<Int>(0)
    val sortMode = MutableLiveData<Int>(0)
    val page = MutableLiveData<Int>(0)
    val ret = MutableLiveData<JsonResult<SeartchRet>>()

    fun getData() {
        if (isOneLoading.value!!)
            return
        hold({
            var priceStatus = ""
            var salesStatus = ""
            when (currMode.value) {
                1 -> {
                    if (sortMode.value == 1) {
                        priceStatus = "0"
                    } else {
                        priceStatus = "1"
                    }
                }
                2 -> {
                    if (sortMode.value == 3) {
                        salesStatus = "0"
                    } else {
                        salesStatus = "1"
                    }
                }
            }
            server.searchRet(
                page.value!!, wordVal.value!!,
                priceStatus,
                salesStatus
            )
        }, {
            isOneLoading.value = true
            page.value = page.value!! + 1
        }, {
            ret.value = it
        }, complete = {
            isOneLoading.value = false
        })
    }

}